package com.s666.demo.controller;

import com.s666.demo.model.dto.ZikuDTO;
import com.s666.demo.utils.AesCBC;
import com.s666.demo.utils.Pager;
import com.s666.demo.exception.CommonException;
import com.s666.demo.exception.JsonException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
 * @author lijin
 */
@Controller
@RequestMapping("/test")
@Slf4j
public class TestController extends BaseController {

    @RequestMapping("/log")
    public String log() {
        log.info("hello");
        return "default/msg";
    }
    /**
     * 模拟页面异常
     * @return
     */
    @RequestMapping("/index")
    @ResponseBody
    public String index() {
        throw new CommonException("1001","common异常错误");
    }

    /**
     * 模拟json异常
     * @return
     */
    @RequestMapping("/json")
    public String json() {
        throw new JsonException("1002","json错误");
    }

    /**
     * 模拟运行时异常
     * @return
     */
    @RequestMapping("/500")
    @ResponseBody
    public String e500() {
        return 1/0 + "";
    }

    /**
     * 获取分页参数
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/page")
    @ResponseBody
    public String testGet(HttpServletRequest request,
                         HttpServletResponse response) {

        Pager pager = new Pager();
        String queryString = pager.getLink(request);
        return queryString;
    }
    /**
     * 模拟页面异常
     * @return
     */
    @RequestMapping("/test_aes")
    @ResponseBody
    public String testAes() {
        try {
            String key = "6ed7ba0d1211f740a3f0ddd8bd4d2f24";
            String encrypted = AesCBC.encrypt("Hello World", key); // 使用默认密钥
            System.out.println("加密结果: " + encrypted);

            String decrypted = AesCBC.decrypt(encrypted, key);
            System.out.println("解密结果: " + decrypted);

            String decrypted2 = AesCBC.decrypt("gGj7VSBZZFQ9V4IdaUxRnA==","6ed7ba0d1211f740a3f0ddd8bd4d2f24");
            System.out.println("解密结果: " + decrypted2);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
